package passwd

import (
	"golang.org/x/crypto/bcrypt"
	"log"
)

// 专用于账号密码保存于验证
// bcrypt加密后的密码哈希字符串长度固定为60字符，数据库字段设置varchar(60)，格式类似：
// $2a$10$N9qo8uLOickgx2ZMRZoMy.Mrq6phx5U4y7d3R7lWJbHvJYV1nX1Oe

func PasswordHash(password string) string {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		log.Println("passwd.PasswordHash()失败", err)
		return ""
	}
	return string(bytes)
}

func PasswordVerify(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}
